package com.pinyoumei.app.activity.giftbag;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivityCurrency;
import com.pinyoumei.app.activity.ProductDetailActivity;
import com.pinyoumei.app.activity.ProductDetailPtActivity;
import com.pinyoumei.app.adapter.UserBalanceAdapter;
import com.pinyoumei.app.bean.FirstBean;
import com.pinyoumei.app.bean.GiftBagBean;
import com.pinyoumei.app.bean.GoodDetailsBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GiftBagActivity extends BaseActivityCurrency {

    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView gbRecycler;
    private int page = 1;
    private GiftBagAdapter giftBagAdapter;
    private List<GiftBagBean.ResultBean.GiftBagDto> giftBagDtoList = new ArrayList<>();
    private ImageView giftBagHomeImg;

    @Override
    protected int layoutId() {
        return R.layout.gift_bag_view;
    }

    @Override
    protected void initView() {
        smartRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        giftBagHomeImg = findViewById(R.id.gift_bag_home_img);
        gbRecycler = findViewById(R.id.gb_recycler);
        gbRecycler.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        giftBagAdapter = new GiftBagAdapter(R.layout.gift_bag_item_view,null);
        giftBagAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        gbRecycler.setAdapter(giftBagAdapter);
        findViewById(R.id.gb_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        giftBagAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                InitGoodDate(giftBagAdapter.getData().get(position).getId());
            }
        });

        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                giftgoodslist();
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                giftBagDtoList.clear();
                giftgoodslist();
                refreshLayout.finishRefresh();
            }
        });
    }

    @Override
    protected void initData() {
        giftgoodslist();
    }

    private void giftgoodslist() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(GiftBagActivity.this));
        map.put("page", page);
        map.put("limit", 10);
        OkGo.<String>post(Constants.giftgoodslist)
                .params("user_id", SPUtils.getAppUserId(GiftBagActivity.this))
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        GiftBagBean giftBagBean = gson.fromJson(response.body(), GiftBagBean.class);
                        Glide.with(GiftBagActivity.this).load(giftBagBean.getResult().getTop_banner_img()).into(giftBagHomeImg);
                        if (giftBagBean.getCode() >= 0) {
                            giftBagDtoList.addAll(giftBagBean.getResult().getGoods_list());
                            giftBagAdapter.setNewData(giftBagDtoList);
                        } else {
                            Toast.makeText(GiftBagActivity.this, giftBagBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });


    }

    private void InitGoodDate(String goods_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(GiftBagActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.GOODS_LIST_DETAIL)
                .params("user_id", SPUtils.getAppUserId(GiftBagActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        GoodDetailsBean goodDetailsBean = gson.fromJson(response.body(), GoodDetailsBean.class);

                        if (goodDetailsBean.getCode() >= 0) {
                            Utils.isActivity = "3";
                            if (goodDetailsBean.getResult().getData_info().getIs_can_pt() == 1) {
                                Intent intent = new Intent(GiftBagActivity.this, ProductDetailPtActivity.class);
                                intent.putExtra("goodDetailsBean", goodDetailsBean);
                                intent.putExtra("goods_id", goods_id);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(GiftBagActivity.this, ProductDetailActivity.class);
                                intent.putExtra("goodDetailsBean", goodDetailsBean);
                                intent.putExtra("goods_id", goods_id);
                                startActivity(intent);
                            }

                        } else {
                            Toast.makeText(GiftBagActivity.this, goodDetailsBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }
}
